home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 18.7 KB | 661 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWTxtShp.cpp
- // Release Version: $ 1.0d1 $
- //
- // Creation Date: 3/28/94
- //
- // Copyright: © 1993, 1994 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef FWTXTSHP_H
- #include "FWTxtShp.h"
- #endif
-
- #ifndef FWSHAPE_H
- #include "FWShape.h"
- #endif
-
- #ifndef FWGC_H
- #include "FWGC.h"
- #endif
-
- #ifndef FWDFAULT_H
- #include "FWDfault.h"
- #endif
-
- #ifndef FWGRUTIL_H
- #include "FWGrUtil.h"
- #endif
-
- #ifndef FWRECSHP_H
- #include "FWRecShp.h"
- #endif
-
- #ifndef FWOVLSHP_H
- #include "FWOvlShp.h"
- #endif
-
- #ifndef FWRRCSHP_H
- #include "FWRRcShp.h"
- #endif
-
- #ifndef FWARCSHP_H
- #include "FWArcShp.h"
- #endif
-
- #ifndef FWLINSHP_H
- #include "FWLinShp.h"
- #endif
-
- #ifndef FWRGNSHP_H
- #include "FWRgnShp.h"
- #endif
-
- // ----- OS Includes -----
-
- #ifndef FWMEMMGR_H
- #include <FWMemMgr.h>
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef _TRNSFORM_
- #include <Trnsform.h>
- #endif
-
- // ----- Macintosh Includes -----
-
- #if defined(FW_BUILD_MAC) && !defined(mathRoutinesIncludes)
- #include <math routines.h>
- #endif
-
- //==============================================================================
- // •• RunTime Info
- //==============================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment fwgraphx
- #endif
-
- //==============================================================================
- // •• class FW_CTextShape
- //==============================================================================
-
- //------------------------------------------------------------------------------
- // FW_CTextShape::FW_CTextShape
- //------------------------------------------------------------------------------
-
- FW_CTextShape::FW_CTextShape() :
- FW_CShape()
- {
- }
-
- //------------------------------------------------------------------------------
- // FW_CTextShape::FW_CTextShape
- //------------------------------------------------------------------------------
-
- FW_CTextShape::FW_CTextShape(long byteCount, const char* text,
- const FW_CPoint& position,
- FW_TextAlignment textAlignment) :
- FW_CShape()
- {
- SetRep(new FW_CTextShapeRep(byteCount, text, position, textAlignment));
- }
-
- //------------------------------------------------------------------------------
- // FW_CTextShape::FW_CTextShape
- //------------------------------------------------------------------------------
-
- FW_CTextShape::FW_CTextShape(const FW_CString& string,
- const FW_CPoint& position,
- FW_TextAlignment textAlignment) :
- FW_CShape()
- {
- SetRep(new FW_CTextShapeRep(string, position, textAlignment));
- }
-
- //------------------------------------------------------------------------------
- // FW_CTextShape::FW_CTextShape
- //------------------------------------------------------------------------------
-
- FW_CTextShape::FW_CTextShape(const FW_CTextShape& other) :
- FW_CShape(other)
- {
- }
-
- //------------------------------------------------------------------------------
- // FW_CTextShape::FW_CTextShape
- //------------------------------------------------------------------------------
-
- FW_CTextShape::FW_CTextShape(FW_CTextShapeRep* rep) :
- FW_CShape(rep)
- {
- }
-
- //------------------------------------------------------------------------------
- // FW_CTextShape::operator=
- //------------------------------------------------------------------------------
-
- FW_CTextShape& FW_CTextShape::operator=(const FW_CTextShape& other)
- {
- SetRep(other.GetRep());
- return *this;
- }
-
- //------------------------------------------------------------------------------
- // FW_CTextShape::operator=
- //------------------------------------------------------------------------------
-
- FW_CTextShape& FW_CTextShape::operator=(FW_CTextShapeRep* other)
- {
- SetRep(other);
- return *this;
- }
-
- //------------------------------------------------------------------------------
- // FW_CTextShape::operator FW_CRectShape
- //------------------------------------------------------------------------------
-
- FW_CTextShape::operator FW_CRectShape() const
- {
- FW_CRectShape rectShape(((FW_CShapeRep*)GetRep())->GetShapeBounds());
- rectShape->SetShapeFill(((FW_CShapeRep*)GetRep())->GetShapeFill());
- return rectShape;
- }
-
- //------------------------------------------------------------------------------
- // FW_CTextShape::operator FW_COvalShape
- //------------------------------------------------------------------------------
-
- FW_CTextShape::operator FW_COvalShape() const
- {
- FW_COvalShape ovalShape(((FW_CShapeRep*)GetRep())->GetShapeBounds());
- ovalShape->SetShapeFill(((FW_CShapeRep*)GetRep())->GetShapeFill());
- return ovalShape;
- }
-
- //------------------------------------------------------------------------------
- // FW_CTextShape::operator FW_CRoundRectShape
- //------------------------------------------------------------------------------
-
- FW_CTextShape::operator FW_CRoundRectShape() const
- {
- FW_CRoundRectShape roundRectShape(((FW_CShapeRep*)GetRep())->GetShapeBounds(), FW_kZeroPoint);
- roundRectShape->SetShapeFill(((FW_CShapeRep*)GetRep())->GetShapeFill());
- return roundRectShape;
- }
-
- //------------------------------------------------------------------------------
- // FW_CTextShape::operator FW_CArcShape
- //------------------------------------------------------------------------------
-
- FW_CTextShape::operator FW_CArcShape() const
- {
- FW_CArcShape arcShape(((FW_CShapeRep*)GetRep())->GetShapeBounds(), 0, 0);
- arcShape->SetShapeFill(((FW_CShapeRep*)GetRep())->GetShapeFill());
- return arcShape;
- }
-
- //------------------------------------------------------------------------------
- // FW_CTextShape::operator FW_CLineShape
- //------------------------------------------------------------------------------
-
- FW_CTextShape::operator FW_CLineShape() const
- {
- FW_CPoint start, end;
- start = ((FW_CTextShapeRep*)GetRep())->GetPosition();
- end.y = start.y;
-
- FW_CRect bounds = ((FW_CShapeRep*)GetRep())->GetShapeBounds();
- end.x = bounds.right - ((FW_CShapeRep*)GetRep())->GetPenSize();
-
- FW_CLineShape lineShape(start, end);
- lineShape->SetShapeFill(((FW_CShapeRep*)GetRep())->GetShapeFill());
- return lineShape;
- }
-
- //------------------------------------------------------------------------------
- // FW_CTextShape::operator FW_CRegionShape
- //------------------------------------------------------------------------------
-
- FW_CTextShape::operator FW_CRegionShape() const
- {
- FW_CRegionShape regionShape(((FW_CShapeRep*)GetRep())->GetShapeBounds());
- regionShape->SetShapeFill(((FW_CShapeRep*)GetRep())->GetShapeFill());
- return regionShape;
- }
-
- //==============================================================================
- // •• class FW_CTextShapeRep
- //==============================================================================
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::FW_CTextShapeRep
- //------------------------------------------------------------------------------
-
- FW_CTextShapeRep::FW_CTextShapeRep() :
- FW_CShapeRep(gGraphicGlobales.gText),
- fByteCount(0),
- fText(NULL),
- fPosition(0, 0),
- fTextAlignment(FW_kBaseLine)
- {
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::FW_CTextShapeRep
- //------------------------------------------------------------------------------
-
- FW_CTextShapeRep::FW_CTextShapeRep(long byteCount, const char* text,
- const FW_CPoint& position,
- FW_TextAlignment textAlignment) :
- FW_CShapeRep(gGraphicGlobales.gText),
- fByteCount(byteCount),
- fText(NULL),
- fPosition(position),
- fTextAlignment(textAlignment)
- {
- if (text == NULL)
- fByteCount == 0;
-
- if (fByteCount != 0)
- {
- fText = (char*)FW_CMemoryManager::AllocateBlock(byteCount);
- FW_CMemoryManager::CopyMemory(text, fText, byteCount);
- }
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::FW_CTextShapeRep
- //------------------------------------------------------------------------------
-
- FW_CTextShapeRep::FW_CTextShapeRep(const FW_CString& string,
- const FW_CPoint& position,
- FW_TextAlignment textAlignment) :
- FW_CShapeRep(gGraphicGlobales.gText),
- fByteCount(string.GetByteLength()),
- fText(NULL),
- fPosition(position),
- fTextAlignment(textAlignment)
- {
- if (fByteCount != 0)
- {
- fText = (char*)FW_CMemoryManager::AllocateBlock(fByteCount);
- FW_CMemoryManager::CopyMemory((const char*)string, fText, fByteCount);
- // string.Export(fText);
- }
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::~FW_CTextShapeRep
- //------------------------------------------------------------------------------
-
- FW_CTextShapeRep::~FW_CTextShapeRep()
- {
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::CalcTextPosition
- //------------------------------------------------------------------------------
-
- FW_SPlatformPoint FW_CTextShapeRep::CalcTextPosition(FW_CGraphicContext* graphicContext,
- const FW_CPoint& position,
- FW_TextAlignment textAlignment)
- {
- FW_CPoint tempPt(position);
-
- #ifdef FW_BUILD_MAC
- FontInfo fi;
- switch (textAlignment)
- {
- case FW_kAscentLine:
- ::GetFontInfo(&fi);
- tempPt.y += ff(fi.ascent);
- break;
-
- case FW_kBaseLine:
- break;
-
- case FW_kDescentLine:
- ::GetFontInfo(&fi);
- tempPt.y -= ff(fi.descent + fi.leading);
- break;
- }
- #endif
-
- return graphicContext->AsPlatformPoint(tempPt);
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::Draw
- //------------------------------------------------------------------------------
-
- void FW_CTextShapeRep::Draw(FW_CGraphicContext* graphicContext)
- {
- FW_ShapeFills shapeFill = GetShapeFill();
-
- if (shapeFill == FW_kNullShape || fText == NULL)
- return;
-
- graphicContext->SelectInkAndStyle(GetShapeInk(), GetShapeStyle(), FW_kTypographicShape, shapeFill);
-
- FW_SPlatformPoint position = CalcTextPosition(graphicContext, fPosition, fTextAlignment);
-
- #ifdef FW_BUILD_MAC
- ::MoveTo(position.h, position.v);
- ::DrawText(fText, 0, fByteCount);
- #endif
-
- #ifdef FW_BUILD_WIN
- #endif
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::Draw
- //------------------------------------------------------------------------------
-
- void FW_CTextShapeRep::Draw(FW_CGraphicContext* graphicContext,
- const FW_CRect& box,
- FW_Justification justification)
- {
- FW_ShapeFills shapeFill = GetShapeFill();
-
- if (shapeFill == FW_kNullShape || fText == NULL)
- return;
-
- graphicContext->SelectInkAndStyle(GetShapeInk(), GetShapeStyle(), FW_kTypographicShape, shapeFill);
-
- #ifdef FW_BUILD_MAC
- FontInfo theFontInfo;
- XMPCoordinate widthOfString;
- XMPCoordinate boxWidth;
- FW_CRect localBox = box;
-
- ::GetFontInfo(&theFontInfo);
- widthOfString = ff(::TextWidth(fText, 0, fByteCount));
- boxWidth = localBox.right - localBox.left;
- if (widthOfString < boxWidth)
- {
- switch (justification)
- {
- case FW_kFlushLeft:
- case FW_kFlushDefault:
- break;
-
- case FW_kFlushCenter:
- localBox.left += (boxWidth - widthOfString) / 2;
- break;
-
- case FW_kFlushRight:
- localBox.left += boxWidth - widthOfString;
- break;
- }
- }
-
- localBox.top += ff(theFontInfo.ascent) + (((localBox.bottom - localBox.top) - ff(theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading)) / 2) ;
- FW_SPlatformPoint qdPos = graphicContext->AsPlatformPoint(localBox[FW_kTopLeft]);
- ::MoveTo(qdPos.h, qdPos.v);
- ::DrawText(fText, 0, fByteCount);
- #endif
- #ifdef FW_BUILD_WIN
- #endif
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::DrawText
- //------------------------------------------------------------------------------
-
- void FW_CTextShapeRep::DrawText(FW_CGraphicContext* graphicContext,
- long byteCount,
- const char* text,
- const FW_CPoint& position,
- FW_TextAlignment textAlignment)
- {
- if (text == NULL || byteCount == 0 || gGraphicGlobales.gText.fFill == FW_kNullShape)
- return;
-
- graphicContext->SelectInkAndStyle(gGraphicGlobales.gText.fInk, gGraphicGlobales.gText.fStyle, FW_kTypographicShape, gGraphicGlobales.gText.fFill);
-
- FW_SPlatformPoint pos = FW_CTextShapeRep::CalcTextPosition(graphicContext, position, textAlignment);
-
- #ifdef FW_BUILD_MAC
- ::MoveTo(pos.h, pos.v);
- ::DrawText(text, 0, byteCount);
- #endif
- #ifdef FW_BUILD_WIN
- #endif
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::DrawString
- //------------------------------------------------------------------------------
-
- void FW_CTextShapeRep::DrawString(FW_CGraphicContext* graphicContext,
- const FW_CString& string,
- const FW_CPoint& position,
- FW_TextAlignment textAlignment)
- {
- if (string.GetByteLength() == 0 || gGraphicGlobales.gText.fFill == FW_kNullShape)
- return;
-
- graphicContext->SelectInkAndStyle(gGraphicGlobales.gText.fInk, gGraphicGlobales.gText.fStyle, FW_kTypographicShape, gGraphicGlobales.gText.fFill);
-
- FW_SPlatformPoint pos = CalcTextPosition(graphicContext, position, textAlignment);
-
- #ifdef FW_BUILD_MAC
- ::MoveTo(pos.h, pos.v);
- ::DrawText(string, 0, string.GetByteLength());
- #endif
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::Transform
- //------------------------------------------------------------------------------
-
- void FW_CTextShapeRep::Transform(XMPTransform* transform)
- {
- fPosition = transform->TransformPoint(fPosition);
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::InverseTransform
- //------------------------------------------------------------------------------
-
- void FW_CTextShapeRep::InverseTransform(XMPTransform* transform)
- {
- fPosition = transform->InvertPoint(fPosition);
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::MoveShape
- //------------------------------------------------------------------------------
-
- void FW_CTextShapeRep::MoveShape(XMPCoordinate deltaX, XMPCoordinate deltaY)
- {
- fPosition.x += deltaX;
- fPosition.y += deltaY;
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::MoveShapeTo
- //------------------------------------------------------------------------------
-
- void FW_CTextShapeRep::MoveShapeTo(XMPCoordinate x, XMPCoordinate y)
- {
- fPosition.x = x;
- fPosition.y= y;
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::GetShapeBounds
- //------------------------------------------------------------------------------
-
- FW_CRect FW_CTextShapeRep::GetShapeBounds() const
- {
- FW_CRect rect(0,0,0,0);
-
- if (fByteCount != 0)
- {
- #ifdef FW_BUILD_MAC
- GrafPtr svPort;
-
- ::GetPort(&svPort);
- ::SetPort(gGraphicGlobales.gScratchWindow);
-
- TextStyle textStyle;
- GetMacTextStyle(textStyle);
- ::SetPortTextStyle(textStyle);
-
- FontInfo fi;
- ::GetFontInfo(&fi);
-
- long width = ::TextWidth(fText, 0, fByteCount);
-
- rect.left = fPosition.x;
- rect.right = fPosition.x + ff(width);
-
- switch (fTextAlignment)
- {
- case FW_kAscentLine:
- rect.top = fPosition.y;
- rect.bottom = rect.top + ff(fi.ascent + fi.descent + fi.leading);
- break;
- case FW_kBaseLine:
- rect.top = fPosition.y - ff(fi.ascent);
- rect.bottom = rect.top + ff(fi.descent + fi.leading);
- break;
- case FW_kDescentLine:
- rect.bottom = fPosition.y;
- rect.top = rect.bottom - ff(fi.ascent + fi.descent + fi.leading);
- break;
- }
-
- ::SetPort(svPort);
- #endif
-
- #ifdef FW_BUILD_WIN
- NotYetImplemented();
- #endif
- }
-
- return rect;
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::Copy
- //------------------------------------------------------------------------------
-
- FW_CTextShape FW_CTextShapeRep::Copy() const
- {
- // fText will be copied
- FW_CTextShape text(fByteCount, fText, fPosition, fTextAlignment);
- return text;
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::GetText
- //------------------------------------------------------------------------------
-
- long FW_CTextShapeRep::GetText(char* text) const
- {
- if (fText == NULL)
- return 0;
-
- FW_CMemoryManager::CopyMemory(fText, text, fByteCount);
- return fByteCount;
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::SetText
- //------------------------------------------------------------------------------
-
- void FW_CTextShapeRep::SetText(long byteCount, const char* text)
- {
- if (text == NULL)
- byteCount = 0;
-
- fByteCount = byteCount;
- if (fText)
- FW_CMemoryManager::FreeBlock(fText);
-
-
- if (fByteCount != 0)
- {
- fText = (char*)FW_CMemoryManager::AllocateBlock(byteCount);
- FW_CMemoryManager::CopyMemory(text, fText, byteCount);
- }
-
- Changed();
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::SetAsDefault
- //------------------------------------------------------------------------------
-
- void FW_CTextShapeRep::SetAsDefault() const
- {
- SetDefaultProperties(gGraphicGlobales.gText);
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::HitTest
- //------------------------------------------------------------------------------
-
- FW_HitTestPart FW_CTextShapeRep::HitTest(const FW_CPoint& test) const
- {
- return FW_kOutside;
- }
-
- //------------------------------------------------------------------------------
- // • FW_CTextShapeRep::GetMacTextStyle
- //------------------------------------------------------------------------------
-
- void FW_CTextShapeRep::GetMacTextStyle(TextStyle& textStyle) const
- {
- XMPCoordinate textSize;
- GetShapeStyle()->GetMacFontInfo(textStyle.tsFont, textStyle.tsFace, textSize);
- textStyle.tsSize = FixedToInt(textSize);
-
- FW_CColor color;
- GetShapeInk()->GetForeColor(&color);
- textStyle.tsColor = color;
- }
-
- //----------------------------------------------------------------------------------------
- // • FW_CTextShapeRep::Flatten
- //----------------------------------------------------------------------------------------
-
- void FW_CTextShapeRep::Flatten(FW_CWritableStream& stream)
- {
- fPosition.Flatten(stream);
- stream.Write((char*)&fTextAlignment, sizeof(fTextAlignment));
- stream << fByteCount;
- stream.Write(fText, fByteCount);
- }
-
- //----------------------------------------------------------------------------------------
- // • FW_CTextShapeRep::Unflatten
- //----------------------------------------------------------------------------------------
-
- void FW_CTextShapeRep::Unflatten(FW_CReadableStream& stream)
- {
- if (fText)
- {
- ::DisposePtr((char*)fText);
- fText = NULL;
- }
- fByteCount = 0;
-
- fPosition.Unflatten(stream);
- stream.Read((char*)&fTextAlignment, sizeof(fTextAlignment));
- stream >> fByteCount;
-
- if (fByteCount != 0)
- {
- fText = (char*)::NewPtr(fByteCount);
- stream.Read(fText, fByteCount);
- }
- }
-